<template>
  <ContentWrap  v-show="query">
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="单据编号" prop="code">
        <el-input
            v-model="queryParams.code"
            placeholder="请输入单据编号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="单据类型" prop="docId">
        <select-single-wms-doc v-model:doc-id="queryParams.docId" v-model:doc-name="queryParams.docName" doc-type="wms_other_in"/>
      </el-form-item>
      <el-form-item label="单据日期" prop="docDate">
        <el-date-picker
          v-model="queryParams.docDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审核日期" prop="approveDate">
        <el-date-picker
            v-model="queryParams.approveDate"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="制单人" prop="operateUser">
        <el-input
          v-model="queryParams.operateUser"
          placeholder="请输入制单人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="approveUser">
        <el-input
          v-model="queryParams.approveUser"
          placeholder="请输入审核人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="审核状态" prop="approveStatus">
        <el-select
          v-model="queryParams.approveStatus"
          placeholder="请选择审核状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.APPROVE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-row >
      <el-col class="mb3">
        <el-button
            type="primary"
            plain
            @click="openForm('create')"
            v-hasPermi="['wms:other-in:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['wms:other-in:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
                  <el-button :icon="RefreshRight" circle @click="handleQuery" />
        <el-button :icon="Search" circle @click="query = !query" />
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list" :stripe="true">
      <el-table-column label="编号" align="center" prop="code" min-width="200" fixed />
      <el-table-column label="单据类型" align="center" prop="docName" />
      <el-table-column
          label="单据日期"
          align="center"
          prop="docDate"
          :formatter="dateFormatter2"
          width="180px"
      />
      <el-table-column label="合计数量" align="center" prop="businessSumQty" />
      <el-table-column label="合计金额" align="center" prop="costSumAmt" />
      <el-table-column label="附件" align="center" prop="annex" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="制单人" align="center" prop="operateUserName" />
      <el-table-column label="制单部门" align="center" prop="operateDeptName" />
      <el-table-column label="审核人" align="center" prop="approveUserName" />
      <el-table-column
          label="审核日期"
          align="center"
          prop="approveDate"
          :formatter="dateFormatter2"
          width="180px"
      />
      <el-table-column label="审核状态" align="center" prop="approveStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.APPROVE_STATUS" :value="scope.row.approveStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="200" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)" v-hasPermi="['wms:other-in:update']">编辑</el-button>
          <el-button link type="primary" @click="handleApprove(scope.row.id)" v-hasPermi="['wms:other-in:approve']" v-show="!scope.row.approveStatus">审核</el-button>
          <el-button link type="primary" @click="handleCancel(scope.row.id)" v-hasPermi="['wms:other-in:cancel']" v-show="scope.row.approveStatus">撤审</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['wms:other-in:delete']" v-show="!scope.row.approveStatus">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OtherInForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import {RefreshRight, Search} from "@element-plus/icons-vue";
import download from '@/utils/download'
import * as OtherInApi from '@/api/wms/otherin'
import OtherInForm from './OtherInForm.vue'
import SelectSingleWmsDoc from "@/views/wms/doc/components/SelectSingleWmsDoc.vue";

defineOptions({ name: 'OtherIn' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const query = ref(false) //搜索框
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  docId: undefined,
  docDate: [],
  code: undefined,
  transactionDate: [],
  operateUser: undefined,
  operateDept: undefined,
  operateDate: [],
  approveUser: undefined,
  approveDate: [],
  businessSumQty: undefined,
  costSumAmt: undefined,
  approveStatus: undefined,
  annex: undefined,
  remark: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OtherInApi.getOtherInPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 审核按钮操作 */
const handleApprove = async (id: number) => {
  try {
    // 审核的二次确认
    await message.approveConfirm()
    // 发起审核
    await OtherInApi.approveOtherIn(id)
    message.success(t('common.approveSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 撤审按钮操作 */
const handleCancel = async (id: number) => {
  try {
    // 撤审的二次确认
    await message.cancelConfirm()
    // 发起撤审
    await OtherInApi.cancelOtherIn(id)
    message.success(t('common.cancelSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OtherInApi.deleteOtherIn(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OtherInApi.exportOtherIn(queryParams)
    download.excel(data, '通用入库单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
